package cn.iocoder.yudao.module.shop.controller.admin.itemtimes;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.shop.controller.admin.item.vo.ShopItemRespVO;
import cn.iocoder.yudao.module.shop.convert.item.ShopItemConvert;
import cn.iocoder.yudao.module.shop.dal.dataobject.item.ShopItemDO;
import cn.iocoder.yudao.module.shop.service.item.ShopItemService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.itemtimes.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.itemtimes.ShopItemTimesDO;
import cn.iocoder.yudao.module.shop.convert.itemtimes.ShopItemTimesConvert;
import cn.iocoder.yudao.module.shop.service.itemtimes.ShopItemTimesService;

@Tag(name = "管理后台 - 店内项目次数")
@RestController
@RequestMapping("/shop/item-times")
@Validated
public class ShopItemTimesController {

    @Resource
    private ShopItemTimesService itemTimesService;

    @Resource
    private ShopItemService shopItemService;

    @PostMapping("/create")
    @Operation(summary = "创建店内项目次数")
    @PreAuthorize("@ss.hasPermission('shop:item-times:create')")
    public CommonResult<Long> createItemTimes(@Valid @RequestBody ShopItemTimesCreateReqVO createReqVO) {
        return success(itemTimesService.createItemTimes(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新店内项目次数")
    @PreAuthorize("@ss.hasPermission('shop:item-times:update')")
    public CommonResult<Boolean> updateItemTimes(@Valid @RequestBody ShopItemTimesUpdateReqVO updateReqVO) {
        itemTimesService.updateItemTimes(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除店内项目次数")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:item-times:delete')")
    public CommonResult<Boolean> deleteItemTimes(@RequestParam("id") Long id) {
        itemTimesService.deleteItemTimes(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店内项目次数")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:item-times:query')")
    public CommonResult<ShopItemTimesRespVO> getItemTimes(@RequestParam("id") Long id) {
        ShopItemTimesDO itemTimes = itemTimesService.getItemTimes(id);
        return success(ShopItemTimesConvert.INSTANCE.convert(itemTimes));
    }

    @GetMapping("/list")
    @Operation(summary = "获得店内项目次数列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:item-times:query')")
    public CommonResult<List<ShopItemTimesRespVO>> getItemTimesList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopItemTimesDO> list = itemTimesService.getItemTimesList(ids);
        return success(ShopItemTimesConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/all")
    @Operation(summary = "获取所有店内项目次数")
    @Parameter(name = "memberId", description = "会员id", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:item-times:query')")
    public CommonResult<List<ShopItemTimesRespVO>> getAllItemList(@RequestParam("memberId") Long membrId){
        List<ShopItemTimesDO> list = itemTimesService.getAll(membrId);
        Set<Long> itemIds = CollectionUtils.convertSet(list, ShopItemTimesDO::getOptItemId);
        List<ShopItemDO> itemDOList = shopItemService.getItemList(itemIds);
        List<ShopItemRespVO> itemRespVOList = ShopItemConvert.INSTANCE.convertList(itemDOList);
        List<ShopItemTimesRespVO> respVOList = ShopItemTimesConvert.INSTANCE.convertList(list);
        Map<Long, ShopItemRespVO> itemRespVOMap = CollectionUtils.convertMap(itemRespVOList, ShopItemRespVO::getId);
        for(int i = 0; i<respVOList.size(); i++){
            respVOList.get(i).setShopItem(itemRespVOMap.get(respVOList.get(i).getOptItemId()));
        }
        return success(respVOList);
    }

    @GetMapping("/page")
    @Operation(summary = "获得店内项目次数分页")
    @PreAuthorize("@ss.hasPermission('shop:item-times:query')")
    public CommonResult<PageResult<ShopItemTimesRespVO>> getItemTimesPage(@Valid ShopItemTimesPageReqVO pageVO) {
        PageResult<ShopItemTimesDO> pageResult = itemTimesService.getItemTimesPage(pageVO);
        return success(ShopItemTimesConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店内项目次数 Excel")
    @PreAuthorize("@ss.hasPermission('shop:item-times:export')")
    @OperateLog(type = EXPORT)
    public void exportItemTimesExcel(@Valid ShopItemTimesExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopItemTimesDO> list = itemTimesService.getItemTimesList(exportReqVO);
        // 导出 Excel
        List<ShopItemTimesExcelVO> datas = ShopItemTimesConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "店内项目次数.xls", "数据", ShopItemTimesExcelVO.class, datas);
    }

}
